package com.shb.game;

import java.util.Random;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class CrazyNumActivity extends BaseActivity implements OnItemClickListener {

	private Button btnBegin;
	private GridView gridMain;
	private Button btnHelp;
	private Button btnTips;
	private Spinner spSelect;
	private TextView tvNum;

	/** 行列数 */
	private int row = 6;
	private int col = 6;
	/** 目标数组 */
	private int[] desInt = new int[row * col];
	/** 选关 */
	private final String[] select = new String[] { "3＊3（简单）", "4＊4（一般）", "5＊5（普通）", "6＊6（较难）", "7＊7（困难）", };
	/** 适配器 */
	private BaseAdapter adapter;
	/** 点击次数 */
	private static int clicknum = 0;
	/** 提示 */
	private int[] lstTips = new int[row * col];
	/** 提示格 */
	private int tip = -1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.crazy_num);
		btnBegin = (Button) findViewById(R.id.btnBegin);
		btnHelp = (Button) findViewById(R.id.btnHelp);
		btnTips = (Button) findViewById(R.id.btnTips);
		gridMain = (GridView) findViewById(R.id.gridMain);
		spSelect = (Spinner) findViewById(R.id.spSelect);
		tvNum = (TextView) findViewById(R.id.tvNum);
		adapter = new BaseAdapter() {
			@Override
			public View getView(int position, View convertView, android.view.ViewGroup parent) {
				TextView btn = new TextView(CrazyNumActivity.this);
				btn.setText(desInt[position] + "");
				btn.setTextColor(Color.BLACK);
				btn.setGravity(Gravity.CENTER);
				btn.setBackgroundResource(R.drawable.btn_bg);
				if (position == tip) {
					btn.setTextColor(Color.RED);
				}
				return btn;
			}

			@Override
			public int getCount() {
				return desInt.length;
			}

			@Override
			public Object getItem(int paramInt) {
				return paramInt;
			}

			@Override
			public long getItemId(int paramInt) {
				return paramInt;
			};
		};
		gridMain.setAdapter(adapter);
		gridMain.setOnItemClickListener(this);
		btnBegin.setOnClickListener(this);
		btnHelp.setOnClickListener(this);
		btnTips.setOnClickListener(this);
		spSelect.setAdapter(new ArrayAdapter(CrazyNumActivity.this, android.R.layout.simple_spinner_item, select));
		spSelect.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> paramAdapterView, View paramView, int paramInt, long paramLong) {
				row = col = paramInt + 3;
				initDate();
			}

			@Override
			public void onNothingSelected(AdapterView<?> paramAdapterView) {
			}

		});
		initDate();
	}

	/** 初始化数据 */
	private void initDate() {
		tip = -1;
		gridMain.setNumColumns(col);
		lstTips = new int[row * col];
		clicknum = 0;
		tvNum.setText(clicknum + "");
		desInt = new int[row * col];
		for (int i = 0; i < row * col * 2 + 1; i++) {
			int r = new Random().nextInt(row * col);
			addTip(r, 3);
			int ro = r / col;
			int co = r % col;
			for (int m = ro - 1; m <= ro + 1; m++) {
				if (m >= 0 && m < row) {
					desInt[m * col + co]++;
					desInt[m * col + co] %= 4;
				}
			}
			for (int m = co - 1; m <= co + 1; m++) {
				if (m >= 0 && m < col) {
					desInt[ro * col + m]++;
					desInt[ro * col + m] %= 4;
				}
			}
			desInt[ro * col + co]--;
			desInt[ro * col + co] += 4;
			desInt[ro * col + co] %= 4;
		}
		adapter.notifyDataSetChanged();
	}

	@Override
	public void onClick(View paramView) {
		switch (paramView.getId()) {
		case R.id.btnBegin:
			initDate();
			break;
		case R.id.btnHelp:
			startActivity(new Intent(this, CrazyNumHelpActivity.class));
			break;
		case R.id.btnTips:
			tip = -1;
			for (int i = 0; i < lstTips.length; i++) {
				if (lstTips[i] > 0) {
					tip = i;
					adapter.notifyDataSetChanged();
					break;
				}
			}
			break;
		}
	}

	/** 记录提示位置 */
	private void addTip(int r, int n) {
		while (n > 0) {
			lstTips[r]++;
			n--;
		}
		for (int i = 0; i < lstTips.length; i++) {
			lstTips[r] %= 4;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> paramAdapterView, View paramView, int paramInt, long paramLong) {
		tip = -1;
		addTip(paramInt, 1);
		int r = paramInt;
		int ro = r / col;
		int co = r % col;
		for (int m = ro - 1; m <= ro + 1; m++) {
			if (m >= 0 && m < row) {
				desInt[m * col + co]--;
				desInt[m * col + co] += 4;
				desInt[m * col + co] %= 4;
			}
		}
		for (int m = co - 1; m <= co + 1; m++) {
			if (m >= 0 && m < col) {
				desInt[ro * col + m]--;
				desInt[ro * col + m] += 4;
				desInt[ro * col + m] %= 4;
			}
		}
		desInt[ro * col + co]++;
		desInt[ro * col + co] %= 4;
		adapter.notifyDataSetChanged();
		clicknum++;
		tvNum.setText(clicknum + "");
		check();
	}

	/** 检查是否完成 */
	private void check() {
		for (int i = 0; i < desInt.length; i++) {
			if (desInt[i] > 0) {
				return;
			}
		}
		Toast.makeText(this, "恭喜你用了" + clicknum + "次完成了！", 0).show();
		initDate();
	}
}
